package org.autocode.controller;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import javafx.stage.Window;
import org.autocode.config.AutoConfig;
import org.autocode.config.AutoConfigFactory;
import org.autocode.config.DatasourceConfig;
import org.autocode.watch.DatasourceData;

/**
 * @program: autocode
 * @description:
 * @author: zhu.xiaojie
 * @create: 2020-08-16 10:58
 */
public class DataSourceController {

    @FXML
    public TextField name;
    @FXML
    public TextField host;
    @FXML
    public TextField username;
    @FXML
    public TextField password;
    @FXML
    public TextField port;

    @FXML
    public void save(ActionEvent event) {
        String sname = name.getText();
        String shost = host.getText();
        String susername = username.getText();
        String spwd = password.getText();
        int iport = Integer.parseInt(port.getText());

        AutoConfig autoConfig = AutoConfigFactory.getAutoConfig();
        DatasourceConfig ds = new DatasourceConfig();
        ds.setName(sname);
        ds.setHost(shost);
        ds.setPort(iport);
        ds.setUsername(susername);
        ds.setPassword(spwd);
        autoConfig.addDatasource(ds);

        DatasourceData datasourceData = DatasourceData.getInstance();
        datasourceData.change();

        Stage window = (Stage) username.getScene().getWindow();
        window.close();
    }
}
